package com.jtlrm.ckd.mvp.view.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.sdk.base.api.PositiveOrCancelInterface;
import com.base.sdk.util.DialogUtil;
import com.base.sdk.util.ImageUtil;
import com.hyphenate.EMCallBack;
import com.hyphenate.chatuidemo.DemoHelper;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.fragment.BaseFragment;
import com.jtlrm.ckd.config.CodeConstants;
import com.jtlrm.ckd.entity.UserEntity;
import com.jtlrm.ckd.entity.event.UserInfoMessage;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.mvp.view.activity.AboutUsActivity;
import com.jtlrm.ckd.mvp.view.activity.LoginActivity;
import com.jtlrm.ckd.mvp.view.activity.MyQRCodeActivity;
import com.jtlrm.ckd.mvp.view.activity.PersonInfoActivity;
import com.jtlrm.ckd.mvp.view.activity.ZhangHaoActivity;
import com.jtlrm.ckd.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;

/**
 * 个人中心
 */
public class PersonFragment extends BaseFragment implements View.OnClickListener {


    @BindViews({R.id.person_1, R.id.person_2, R.id.person_3, R.id.person_4, R.id.person_5})
    List<RelativeLayout> listRelative;
    @BindView(R.id.name)
    TextView nameT;
    @BindView(R.id.avatar)
    ImageView avatarI;
    @BindView(R.id.line_1)
    View line1;

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_person;
    }

    @Override
    protected void initView() {
        EventBusUtil.register(this);
        updateInfo();
    }

    private void updateInfo() {
        UserEntity userEntity = UserHelper.getInstance(context).getUserInfo();
        if (userEntity != null) {
            setText(nameT, userEntity.getName());
            ImageUtil.loadCircleImage(avatarI, userEntity.getHeadPicture(), R.drawable.person_change);
        }
        if (UserHelper.getInstance(context).isDoctor()) {
            listRelative.get(0).setVisibility(View.VISIBLE);
            line1.setVisibility(View.VISIBLE);
        } else {
            line1.setVisibility(View.GONE);
            listRelative.get(0).setVisibility(View.GONE);
        }
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        for (RelativeLayout re : listRelative) {
            re.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.person_1:
                startActivity(new Intent(context, MyQRCodeActivity.class));
                break;
            case R.id.person_2:
                startActivity(new Intent(context, PersonInfoActivity.class));
                break;
            case R.id.person_3:
                startActivity(new Intent(context, ZhangHaoActivity.class));
                break;
            case R.id.person_4:
                startActivity(new Intent(context, AboutUsActivity.class));
                break;
            case R.id.person_5:
                DialogUtil.showDialog(context, CodeConstants.LOGOUT, new PositiveOrCancelInterface() {
                    @Override
                    public void result(boolean result) {
                        if (result) {
                            loginout();
                        }
                    }
                });
                break;
        }
    }

    public void loginout() {
        showLoadingDialog("退出中");
        UserHelper.getInstance(context).clearData();
        DemoHelper.getInstance().logout(true, new EMCallBack() {

            @Override
            public void onSuccess() {
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        dismissLoadingDialog();
                        getActivity().finish();
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                    }
                });
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        dismissLoadingDialog();
                        getActivity().finish();
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                    }
                });
            }
        });
    }

    @Override
    protected boolean isImmersionBarEnabled() {
        return true;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UserInfoMessage event) {
        if (event.isUpdateMesaage()) {
            updateInfo();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }
}
